home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / forms / FORMS / timer.c < prev   
C/C++ Source or Header  |  1994-08-01  |  4KB  |  167 lines

  1. /*
  2.  * timer.c
  3.  *
  4.  * Forms Object class: TIMER
  5.  *
  6.  * Written by: Mark Overmars
  7.  *
  8.  * Version 2.1 a
  9.  * Date: Sep 29,  1992
  10.  */
  11.  
  12. #include <malloc.h>
  13. #include <stdio.h>
  14. #include <strings.h>
  15. #include <sys/types.h>
  16. #include <sys/time.h>
  17. #include "forms.h"
  18.  
  19. /* Extra information need for input boxes. */
  20. typedef struct {
  21.    int on;            /* whether running */
  22.    long lastsec,lastusec;    /* last time the delay was adapted */
  23.    float delay;            /* the time left to wait */
  24. } SPEC;
  25.  
  26. static void get_time(long *sec, long *usec)
  27. /* returns current time */
  28. {
  29.   struct timeval tp;
  30.   struct timezone tzp;
  31.   gettimeofday(&tp,&tzp);
  32.   *sec = tp.tv_sec;
  33.   *usec = tp.tv_usec;
  34. }
  35.  
  36. static void draw_timer(FL_OBJECT *ob)
  37. /* draws the timer */
  38. {
  39.   int tt;
  40.   int col;
  41.   char str[32];
  42.   SPEC *sp = ((SPEC *)(ob->spec));
  43.   if (!sp->on || sp->delay>0.0)
  44.     col = ob->col1;
  45.   else if ((int) (sp->delay / FL_TIMER_BLINKRATE) % 2)
  46.     col = ob->col1;
  47.   else
  48.     col = ob->col2;
  49.   fl_drw_box(ob->boxtype,ob->x,ob->y,ob->w,ob->h,col,FL_TIMER_BW);
  50.   if (ob->type == FL_VALUE_TIMER && ob->align == FL_ALIGN_CENTER)
  51.     fl_drw_text_beside(FL_ALIGN_LEFT,ob->x,ob->y,ob->w,ob->h,
  52.             ob->lcol,ob->lsize,ob->lstyle,ob->label);
  53.   else
  54.     fl_drw_text_beside(ob->align,ob->x,ob->y,ob->w,ob->h,
  55.             ob->lcol,ob->lsize,ob->lstyle,ob->label);
  56.   if (ob->type == FL_VALUE_TIMER && sp->delay>0.0)
  57.   {
  58.     if (sp->delay <60.0)
  59.       sprintf(str,"%.1f",sp->delay);
  60.     else
  61.     {
  62.       tt = (int) ((sp->delay+0.05) / 60.0);
  63.       sprintf(str,"%d:%2.1f", tt, sp->delay - 60.0 * tt);
  64.     }
  65.     fl_drw_text(FL_ALIGN_CENTER,ob->x,ob->y,ob->w,ob->h,
  66.             ob->lcol,ob->lsize,ob->lstyle,str);
  67.   }
  68. }
  69.  
  70. static int handle_timer(FL_OBJECT *ob,int event,float mx,float my,char key)
  71. /* Handles an event */
  72. {
  73.   SPEC *sp = ((SPEC *)(ob->spec));
  74.   long sec,usec;
  75.   float lastdelay;
  76.   switch (event)
  77.   {
  78.     case FL_DRAW:
  79.     if (ob->type != FL_HIDDEN_TIMER) draw_timer(ob);
  80.         return 0;
  81.     case FL_RELEASE:
  82.     if (ob->type != FL_HIDDEN_TIMER && sp->delay <0.0)
  83.       fl_set_timer(ob,0.0);
  84.     return 0;
  85.     case FL_STEP:
  86.     if (! sp->on) return 0;
  87.         lastdelay = sp->delay;
  88.     get_time(&sec,&usec);
  89.     sp->delay -= (float) (sec - sp->lastsec) +
  90.             (float) (usec - sp->lastusec) / 1000000.0;
  91.     sp->lastsec = sec; sp->lastusec = usec;
  92.         if (sp->delay > 0.0)
  93.     {
  94.       if (ob->type==FL_VALUE_TIMER &&
  95.          (int)(10.0*sp->delay) != (int)(10.0*lastdelay))
  96.         fl_redraw_object(ob);
  97.     }
  98.     else if (lastdelay > 0.0)
  99.     {
  100.       if (ob->type == FL_HIDDEN_TIMER)
  101.         fl_set_timer(ob,0.0);
  102.        else
  103.         fl_redraw_object(ob);
  104.       return 1;
  105.         }
  106.         else if ( (int) (lastdelay / FL_TIMER_BLINKRATE) !=
  107.             (int) (sp->delay / FL_TIMER_BLINKRATE) )
  108.       fl_redraw_object(ob);
  109.     return 0;
  110.     case FL_FREEMEM:
  111.     free(ob->spec);
  112.     return 0;
  113.   }
  114.   return 0;
  115. }
  116.  
  117. /*------------------------------*/
  118.  
  119. FL_OBJECT *fl_create_timer(int type, float x, float y,
  120.                 float w, float h, char label[])
  121. /* creates an object */
  122. {
  123.   FL_OBJECT *ob;
  124.   ob = fl_make_object(FL_TIMER,type,x,y,w,h,label,handle_timer);
  125.   ob->boxtype = FL_TIMER_BOXTYPE;
  126.   ob->col1 = FL_TIMER_COL1;
  127.   ob->col2 = FL_TIMER_COL2;
  128.   ob->align = FL_TIMER_ALIGN;
  129.   ob->lcol = FL_TIMER_LCOL;
  130.   ob->automatic = 1;
  131.  
  132.   ob->spec = (int *) fl_malloc(sizeof(SPEC));
  133.   ((SPEC *)(ob->spec))->delay = 0.0;
  134.   ((SPEC *)(ob->spec))->on = 0;
  135.  
  136.   return ob;
  137. }
  138.  
  139. FL_OBJECT *fl_add_timer(int type, float x, float y,
  140.                 float w, float h, char label[])
  141. /* Adds an object */
  142. {
  143.   FL_OBJECT *ob;
  144.   ob = fl_create_timer(type,x,y,w,h,label);
  145.   fl_add_object(fl_current_form,ob);
  146.   return ob;
  147. }
  148.  
  149. void fl_set_timer(FL_OBJECT *ob, float delay)
  150. /* Sets the timer clock to the particular delay. (0.0 to reset) */
  151. {
  152.   SPEC *sp = ((SPEC *)(ob->spec));
  153.   sp->delay = delay;
  154.   sp->on = (delay>0.0);
  155.   get_time(&(sp->lastsec),&(sp->lastusec));
  156.   if (ob->type != FL_HIDDEN_TIMER) fl_redraw_object(ob);
  157. }
  158.  
  159. float fl_get_timer(FL_OBJECT *ob)
  160. /* returns the amount of time left */
  161. {
  162.    if (((SPEC *)(ob->spec))->delay >0.0)
  163.      return ((SPEC *)(ob->spec))->delay;
  164.    else
  165.      return 0.0;
  166. }
  167.